package com.aids_malaria.http;


import com.aids_malaria.app.App;
import com.aids_malaria.http.api.RetrofitApi;
import com.aids_malaria.util.LogUtil;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Author: Administrator
 * Date: 2019-04-09
 * Description: retrofit的工厂类
 */

public class RetrofitFactory {

    private static final int DEFAULT_TIMEOUT_READ = 90;
    private static final int DEFAULT_TIMEOUT_WRITE = 90;
    private static final int DEFAULT_TIMEOUT_CONNECT = 60; //连接最长时间

//     public static String sBaseUrl = "http://29te143717.zicp.vip/GZYSPAM/"; //base url
//    public static String sBaseUrl = "http://stevenzlny.uicp.net/GZYSPAM/"; //base url
    public static String sBaseUrl = "http://gzynj.5gzvip.idcfengye.com/GZYSPAM/"; //base url

    private static RetrofitFactory sRetrofitFactory;
    private static OkHttpClient sOkHttpClient;
    private static Retrofit sRetrofit;

    // 构造私有化
    private RetrofitFactory(){
        sOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(DEFAULT_TIMEOUT_CONNECT, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT_READ, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT_WRITE, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .cache(new Cache(new File(App.getInstance()
                        .getCacheDir(),""),1024*1024*10))
                .addInterceptor(InterceptorUtil.getHeadInterceptor())
                .addInterceptor(InterceptorUtil.getLogInterceptor())
                .addInterceptor(InterceptorUtil.getCacheInterceptor())
                .addNetworkInterceptor(InterceptorUtil.getCacheInterceptor())
                .build();
        sRetrofit = new Retrofit.Builder()
                .baseUrl(sBaseUrl)
                .client(sOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    /**
     * 单例获取
     * @return
     */
    public static RetrofitFactory getInstance(){
        if(sRetrofitFactory == null){
            synchronized (RetrofitFactory.class){
                if(sRetrofitFactory == null){
                    sRetrofitFactory = new RetrofitFactory();
                }
            }
        }
        return sRetrofitFactory;
    }

    /**
     * 创建API
     *
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> T createApi(Class<T> tClass) {
        sRetrofit = new Retrofit.Builder()
                .baseUrl(sBaseUrl)
                .client(sOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return sRetrofit.create(tClass);
    }

    /**
     * 创建API
     *
     * @param baseUrl 多个BaseURL切换
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> T createApi(String baseUrl, Class<T> tClass) {
        sRetrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(sOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return sRetrofit.create(tClass);
    }

    /**
     * 返回retrofitApi
     * @return
     */
    public RetrofitApi retrofitApi(){
        return getInstance().createApi(RetrofitApi.class);
    }

}
